package com.shujia.collections_;

import org.junit.Test;

import java.io.File;
import java.util.*;

public class Exer1 {

    /*
    4. 对一个Java源文件中的关键字进行计数。
提示：Java源文件中的每一个单词，需要确定该单词是否是一个关键字。为了高效处理这个问题，将所有的关键字保存在一个HashSet中。用contains() 来测试。

     */
    @Test
    public void test4() throws Exception {
        File file = new File("src/com/shujia/collections_/Exer1.java");
        List list = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "if", "implements", "import", "int", "interface", "instanceof", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while");

        Set set = new HashSet();
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) {
            String word = scanner.next();
//            System.out.println(word);

            boolean flag = list.contains(word);
            if (flag) {
                set.add(word);
            }
        }
        System.out.println(set);
        System.out.println(set.size());
    }


    //    统计List集合中每个元素出现的次数
    @Test
    public void test3() {
        List<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 1; i <= 10; i++) {
            int num = random.nextInt(10);
            list.add(num);
        }

        System.out.println(list);
        Set<Integer> set = new HashSet<>();
        set.addAll(list);

        for (Integer num : set) {
            //    int frequency(Collection，Object)：返回指定集合中指定元素的出现次数
            int time = Collections.frequency(list, num);
            System.out.println("数字:" + num + "出现了" + time + "次");
        }

    }

    @Test
    public void test2() {
        Set<Integer> set = new HashSet<>();
        Random random = new Random();
        for (int i = 1; i <= 10; i++) {
            int num = random.nextInt(10);
            set.add(num);
        }
        System.out.println(set);
//    Object max(Collection)：根据元素的自然顺序，返回给定集合中的最大元素
        System.out.println(Collections.max(set));

//    Object max(Collection，Comparator)：根据 Comparator 指定的顺序，返回给定集合中的最大元素
        Integer num = Collections.max(set, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                // 倒序的最大值, 就是最小值
                return -o1.compareTo(o2);
            }
        });
        System.out.println(num);

//    Object min(Collection)
        System.out.println(Collections.min(set));

//    Object min(Collection，Comparator)
        num = Collections.min(set, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                // 倒序的最小值, 就是最大值
                return -o1.compareTo(o2);
            }
        });
        System.out.println(num);


//    boolean replaceAll(List list，Object oldVal，Object newVal)：使用新值替换 List 对象的所有旧值

    }


    //请从键盘随机输入10个整数保存到List中，并按倒序、从大到小的顺序显示出来
    @Test
    public void test1() {
        List<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 1; i <= 10; i++) {
            int num = random.nextInt(10);
            list.add(num);
        }
        System.out.println(list);

        // 并按倒序
        Collections.reverse(list);
        System.out.println(list);

//        sort(List)：根据元素的自然顺序对指定 List 集合元素按升序排序
        Collections.sort(list);
        System.out.println(list);

        // 从大到小排序
//        sort(List，Comparator)：根据指定的 Comparator 产生的顺序对 List 集合元素进行排序
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return -o1.compareTo(o2);
            }
        });
        System.out.println(list);


        // swap(List，int， int)：将指定 list 集合中的 i 处元素和 j 处元素进行交换
        Collections.swap(list, 0, list.size() - 1);
        System.out.println(list);


//        shuffle(List)：对 List 集合元素进行随机排序
        Collections.shuffle(list);
        System.out.println(list);

        //    void copy(List dest,List src)：将src中的内容复制到dest中，如果dest的容量不够会报错
        // size() 返回的元素的个数
        // List<Integer> dest = new ArrayList<>(10);
        Integer[] arr = new Integer[list.size()];
        List<Integer> dest = Arrays.asList(arr);
        System.out.println(list.size());
        System.out.println(dest.size());
        Collections.copy(dest, list);
        System.out.println(dest);

        // boolean replaceAll(List list，Object oldVal，Object newVal)：使用新值替换 List 对象的所有旧值
        Collections.replaceAll(list, 0, 20);
        System.out.println(list);


    }
}
